import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { setupSwagger } from './config/swagger.config';
import { ValidationPipe } from '@nestjs/common';
import { HttpExceptionFilter } from './filters/http-exception.filter';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  // 添加全局验证管道
  app.useGlobalPipes(
    new ValidationPipe({
      transform: true,
      whitelist: true,
      forbidNonWhitelisted: true,
    }),
  );

  // 添加全局异常过滤器
  app.useGlobalFilters(new HttpExceptionFilter());

  // 允许跨域请求
  app.enableCors();

  // 配置Swagger文档
  setupSwagger(app);

  // 启动应用
  const port = process.env.PORT ?? 3000;
  await app.listen(port);

  console.log(`应用已启动: ${await app.getUrl()}`);
  console.log(`Swagger文档: ${await app.getUrl()}/api-docs`);
}

bootstrap().catch((err) => {
  console.error(err);
  process.exit(1);
});
